//
//  CCToast.m
//  JadeShop
//
//  Created by mac on 2019/2/28.
//  Copyright © 2019年 SZJ.test. All rights reserved.
//

#import "CCToast.h"
#import "Tool.h"

static CCToast *_toast = nil;

@interface CCToast()
@property (nonatomic,strong) UIView * backView;
@property (nonatomic,strong) UILabel * messageLabel;
@end


@implementation CCToast


+(instancetype)share{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _toast = [[CCToast alloc] init];
    });
    return _toast;
}


+ (void)showMessage:(NSString *)message{
    CGSize size = [message sizeForFont:[UIFont systemFontOfSize:12] size:CGSizeMake(YYScreenWidth() - 60, CGFLOAT_MAX) mode:NSLineBreakByWordWrapping];
    CCToast *toast = [CCToast share];
    toast.backView.frame = CGRectMake((YYScreenWidth() - size.width) / 2 - 10, CC_FRAME_X(YYScreenHeight() - size.height - 89), size.width + 20, size.height + 20);
    toast.messageLabel.frame = CGRectMake(10, 10, size.width, size.height);
    toast.messageLabel.text = message;
    
    if (!toast.backView.superview){
        
        [[UIApplication sharedApplication].keyWindow addSubview:toast.backView];
        
    }else{
        [NSObject cancelPreviousPerformRequestsWithTarget:toast selector:@selector(removeForSuperView) object:nil];
    }
    
    [toast performSelector:@selector(removeForSuperView) withObject:nil afterDelay:2];
}

-(void)removeForSuperView{
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.alpha = 0;
    } completion:^(BOOL finished) {
        self.backView.alpha = 1;
        [self.backView removeFromSuperview];
    }];
    
}

-(instancetype)init{
    self = [super init];
    if (self) {
        self.backView = [[UIView alloc] init];
        self.backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.backView.layer.cornerRadius = 5;
        self.messageLabel = [[UILabel alloc] init];
        self.messageLabel.textColor = [UIColor whiteColor];
        self.messageLabel.textAlignment = NSTextAlignmentCenter;
        self.messageLabel.font = [UIFont systemFontOfSize:12];
        [self.backView addSubview:self.messageLabel];
    }
    return self;
}


@end
